<template>
  <div id="center">
    <div class="up">
      <div class="bg-color-black item" v-for="item in titleItem1" :key="item.title">
        <p class="ml-3 colorBlue fw-b">{{item.title}}</p>
        <div>
          <dv-digital-flop :config="item.number" style="width:100%;height:.626rem;" />
        </div>
      </div>
      <div class="bg-color-black item" v-for="item in titleItem2" :key="item.title">
        <p class="ml-3 colorBlue fw-b">{{item.title}}</p>
        <div>
          <dv-digital-flop :config="item.number" style="width:100%;height:.626rem;" />
        </div>
      </div>
    </div>
    <div class="down">
      <div class="ranking bg-color-black">
        <span style="color:#5cd9e8">
          <icon name="align-left"></icon>
        </span>
        <span class="fs-xl text mx-2 mb-1">部门年度用电排行榜</span>
        <dv-scroll-ranking-board :config="ranking" style="height:2.75rem" />
      </div>
      <div class="percent">
        <div class="item bg-color-black">
          <span>今日设备负载率</span>
          <CenterChart :id="rate[0].id" :tips="rate[0].tips" :colorObj="rate[0].colorData" />
        </div>
        <div class="item bg-color-black">
          <span>今日告警处理率</span>
          <CenterChart :id="rate[1].id" :tips="rate[1].tips" :colorObj="rate[1].colorData" />
        </div>
        <div class="water">
          <dv-water-level-pond :config="water" style="height: 1.5rem" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import CenterChart from "@/components/echart/center/centerChartRate";

export default {
  props: {
    chartData: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      chart: null,
      titleItem1: [
        {
          title: "今年累计用电量",
          number: {
            number: [0],
            toFixed: 2,
            content: "{nt}"
          },
          style: {
            fontSize: 10
          }
        },
        {
          title: "本月累计用电量",
          number: {
            number: [0],
            toFixed: 2,
            content: "{nt}"
          }
        },
        {
          title: "今日累计用电量",
          number: {
            number: [0],
            toFixed: 2,
            content: "{nt}"
          }
        }
      ],
      titleItem2: [
        {
          title: "今年累计告警次数",
          number: {
            number: [0],
            toFixed: 1,
            content: "{nt}"
          }
        },
        {
          title: "本月累计告警次数",
          number: {
            number: [0],
            toFixed: 1,
            content: "{nt}"
          }
        },
        {
          title: "本周累计告警次数",
          number: {
            number: [0],
            toFixed: 1,
            content: "{nt}"
          }
        }
      ],
      ranking: {
        data: [],
        carousel: "single",
        unit: "度"
      },
      water: {
        data: [0, 0],
        shape: "roundRect",
        formatter: "{value}%",
        waveNum: 3
      },
      // 通过率和达标率的组件复用数据
      rate: [
        {
          id: "centerRate1",
          tips: 0,
          colorData: {
            textStyle: "#3fc0fb",
            series: {
              color: ["#00bcd44a", "transparent"],
              dataColor: {
                normal: "#03a9f4",
                shadowColor: "#97e2f5"
              }
            }
          }
        },
        {
          id: "centerRate2",
          tips: 0,
          colorData: {
            textStyle: "#67e0e3",
            series: {
              color: ["#faf3a378", "transparent"],
              dataColor: {
                normal: "#ff9800",
                shadowColor: "#fcebad"
              }
            }
          }
        }
      ]
    };
  },
  watch: {
    chartData: {
      handler(val) {
        this.setOptions(val)
      },
      immediate: true,
      deep: true
    }
  },
  components: {
    CenterChart
    // centerChart1,
    // centerChart2
  },mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      if (this.chartData != null) {
       this.setOptions(this.chartData)
      }
    },
    setOptions({content} ={}){
      if(content.availability === undefined || content.availability === 100){
        content.availability = 10000
      }
      this.titleItem1 = [
        {
          title: "今年累计用电量",
          number: {
            number: [content.yearEnergy],
            toFixed: 2,
            content: "{nt}"
          }
        },
        {
          title: "本月累计用电量",
          number: {
            number: [content.monthEnergy],
            toFixed: 2,
            content: "{nt}"
          }
        },
        {
          title: "今日累计用电量",
          number: {
            number: [content.todayEnergy],
            toFixed: 2,
            content: "{nt}"
          }
        }],
      this.titleItem2 = [ {
          title: "今年累计告警次数",
          number: {
            number: [content.yearFault],
            toFixed: 0,
            content: "{nt}"
          }
        },
        {
          title: "本月累计告警次数",
          number: {
            number: [content.monthFault],
            toFixed: 0,
            content: "{nt}"
          }
        },
        {
          title: "本周累计告警次数",
          number: {
            number: [content.weekFault],
            toFixed: 0,
            content: "{nt}"
          }
        }
      ],
      this.rate = [
        {
          id: "centerRate1",
          tips: content.powerRate,
          colorData: {
            textStyle: "#3fc0fb",
            series: {
              color: ["#00bcd44a", "transparent"],
              dataColor: {
                normal: "#03a9f4",
                shadowColor: "#97e2f5"
              }
            }
          }
        },
        {
          id: "centerRate2",
          tips: content.faultRate,
          colorData: {
            textStyle: "#67e0e3",
            series: {
              color: ["#faf3a378", "transparent"],
              dataColor: {
                normal: "#ff9800",
                shadowColor: "#fcebad"
              }
            }
          }
        }
      ],
      this.water= {
        data: [content.availability/100 - 25, content.availability/100],
            shape: "roundRect",
            formatter: "{value}%",
            waveNum: 3
      },
      this.ranking= {
        data: this.getDepts(content.yearDepts),
        carousel: "single",
        unit: "度"
      }
    },
    getDepts(yearDepts)
    {
      const array = []
      for (const dept of yearDepts) {
        const d = {
          name: dept.dept.name,
          value: dept.energy
        }
        array.push(d)
      }
      return array
    }
  }
};
</script>

<style lang="scss" scoped>
#center {
  display: flex;
  flex-direction: column;
  .up {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-around;
    .item {
      border-radius: 0.0625rem;
      padding-top: 0.2rem;
      margin-top: 0.1rem;
      width: 32%;
      height: 0.875rem;
    }
  }
  .down {
    padding: 0.07rem 0.05rem;
    padding-bottom: 0;
    width: 100%;
    display: flex;
    height: 3.1875rem;
    justify-content: space-between;
    .bg-color-black {
      border-radius: 0.0625rem;
    }
    .ranking {
      padding: 0.125rem;
      width: 59%;
    }
    .percent {
      width: 40%;
      display: flex;
      flex-wrap: wrap;
      .item {
        width: 50%;
        height: 1.5rem;
        span {
          margin-top: 0.0875rem;
          display: flex;
          justify-content: center;
        }
      }
      .water {
        width: 100%;
      }
    }
  }
}
</style>
